<?php

namespace App\Providers;

use Illuminate\Support\Str;
use Illuminate\Support\ServiceProvider;
use Illuminate\Contracts\Routing\ResponseFactory;
use Symfony\Component\HttpFoundation\BinaryFileResponse;

class ResponseServiceProvider extends ServiceProvider
{
    /**
     * Register services.
     *
     * @return void
     */
    public function register()
    {
    }

    /**
     * Bootstrap services.
     *
     * @return void
     */
    public function boot(ResponseFactory $factory)
    {
        $factory->macro('download', function($file, $name = null, array $headers = array(), $disposition = 'attachment'){
            $response = new BinaryFileResponse($file, 200, $headers, true);
            if (is_null($name))
            {
                $name = basename($file);
            }
            return $response->setContentDisposition($disposition, $name, Str::ascii($name));
        });
    }
}
